import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/job_ticket/common/common_work_all/common_work_all_controller.dart';
import 'package:get/get.dart';

class CommonSamplingAnalysisController extends GetxController {
  //作业总页面controller
  final workAllPageController = Get.find<CommonWorkAllController>();

  //作业票id
  late String ticketReceiptId;

  //气体分析节点
  List<ExecuteTicketGasList> ticketGasList = [];

  //小票作业状态 0：待开票 1：开票中 2：进行中 3：中断 4：已验收
  late int receiptType;

  @override
  void onInit() {
    super.onInit();
    //初始化页面列表
    initData();
  }

  ///初始化数据
  initData() async {
    //作业票id
    ticketReceiptId = workAllPageController.executeEntity.ticketReceiptId!;
    //小票状态
    receiptType = workAllPageController.receiptType;
    //获取数据-气体分析数据
    final res = await HttpUtil().generateGetRequest<List<ExecuteTicketGasList>>(Interface.getListGasInfoById,
        queryParameters: {'ticketReceiptId': ticketReceiptId}).execute();
    res.onSuccess((result) {
      workAllPageController.executeEntity.ticketGasList = result.data;
    });
    //赋值
    ticketGasList = workAllPageController.executeEntity.ticketGasList!;
    //判断是否有数据
    if (ticketGasList.isEmpty) {
      addGasAnalysis();
    } else {
      //气体分析结果
      workAllPageController.executeEntity.gasUrl = ticketGasList[0].gasUrl;
    }
    update();
  }

  ///取样分析时间选择
  void showDatePicker(ExecuteTicketGasList item) {
    CustomSelectFunction.selectDateTime(onCallBack: (String value) {
      item.sampleDate = value;
      update();
    });
  }

  ///新增气体分析
  void addGasAnalysis() {
    addGasAnalysisList();
    update();
  }

  ///新增气体分析列表
  void addGasAnalysisList() {
    ticketGasList.add(ExecuteTicketGasList()
      ..sampleType = 0
      ..ticketReceiptId = ticketReceiptId
      ..isNewData = true);
  }

  ///删除气体分析
  void deleteGasAnalysis(ExecuteTicketGasList item) {
    ticketGasList.remove(item);
    update();
  }
}
